#include "Ellipse.h"
/**
 * 八分法画圆，每次画八个点
 * center 为圆心
 * */
void EllipsePoint(GLint x, GLint y, Point center)
{
    putPixel(x + center.x, y + center.y);
    putPixel(-x + center.x, y + center.y);
    putPixel(x + center.x, -y + center.y);
    putPixel(-x + center.x, -y + center.y);
    // putPixel(y + center.x, x + center.y);
    // putPixel(-y + center.x, x + center.y);
    // putPixel(y + center.x, -x + center.y);
    // putPixel(-y + center.x, -x + center.y);
}
void MidBresenhamEllipse(Point center, GLint a, GLint b)
{
    GLint x, y, squareA, squareB;
    GLdouble d1, d2;
    x = 0;
    y = b;
    squareA = a * a;
    squareB = b * b;
    d1 = b * b + a * a * (y - 0.5);

    EllipsePoint(x, y, center);
    while (squareB * (x + 1) < squareA  * (y - 0.5))
    {

        if (d1 <= 0)
        {
            d1 += squareB * (2 * x + 3);
            x++;
        }
        else
        {
            d1 += squareB * (2 * x + 3) + squareA   * (-2 * y + 2);
            x++;
            y--;
        }
        EllipsePoint(x, y, center);
    }
    d2=squareB*(x+0.5)*(x+0.5)+squareA*(y-1)*(y-1)-squareA*squareB;
    
    while(y>0){
        
        if (d2<=0) {
            d2+=squareB*(2*x+2)+squareA*(-2*y+3);
            x++;y--;
        }else
        {
            d2+=squareA*(-2*y+3);
            y--;
        }
        EllipsePoint(x, y, center);        
    }
    
}